package com.msb.reference;

import com.msb.Worker;

import java.lang.ref.WeakReference;

/**
 * 弱依赖：堆想没有依赖，且发生一次gc就销毁对象
 */
public class WeakReferenceDemo {
    public static void main(String[] args) {
        Worker worker = new Worker();
        WeakReference<Worker> weak = new WeakReference<>(worker);
        System.out.println("worker: " + worker);
        worker = null;

        System.out.println("before gc: " + weak.get());

        System.gc();
        System.out.println("end gc: " + weak.get());
    }
}
